<template>
    <div class="news-container">
        <div v-for="(item, index) in newsList" :key="item.id" class="news-content" @click="handleItemClick(item, index)">
            <div class="flex items-center">
                <span class="title-color" :style="{ backgroundColor: getClassColor(item.classReal) }">
                    {{ getClassLabel(item.classReal) }}
                </span>
                <h4 style="font-weight: bold;">{{ item.title }}</h4>
            </div>
            <div class="content-middle">{{ item.abstractRea }}</div>
            <div class="content-footer flex justify-between">
                <div class="flex items-center">
                    <img :src="item.coverImage">
                    <span style="margin-right: 15px;">{{ item.siteName }} 由 {{ item.opPerson }} 更新</span>
                    <span style="color:#a8a8a8">{{ item.createTime }}</span>
                </div>  
                <div class="flex items-center">
                    <span style="margin-right: 15px; color: #a8a8a8;">{{ item.pubTime }}</span>
                    <el-button v-if="item.isSelect === 0" type="primary" @click.stop="$emit('add', item.id)">加入待发布</el-button>
                    <el-button v-else type="danger" @click.stop="$emit('remove', item.id)">移除</el-button>
                    <bianji @edit="handleEdit(item)"></bianji>
                </div>
            </div>
        </div>
        <div class="flex justify-end align-center gap-[15px]">
            <p>{{ total }}</p>
            <el-pagination background layout="prev, pager, next" :total="total" :current-page.sync="currentPage"
                :page-size.sync="pageSize" @current-change="handlePageChange" />
        </div>
    </div>
</template>
<script setup>
import { ref, watch } from 'vue'
import bianji from '@/components/ClickOnTheEditPage.vue'
import { classModelList } from '@/enums/index'

const props = defineProps({
  dataList: {
    type: Array,
    default: () => []
  }
})

const emits = defineEmits(['updateNews', 'add', 'remove' ,'click', 'edit'])

// 添加分页相关的响应式变量
const newsList = ref([])  // 初始化为空数组而不是null
const currentPage = ref(1)  // 当前页码
const pageSize = ref(10)    // 每页条数
const total = ref(100)     // 总条数

// 监听 dataList 变化，更新 newsList
watch(() => props.dataList, (newVal) => {
  if (newVal && newVal.length > 0) {
    newsList.value = newVal
  }
}, { immediate: true, deep: true })

// 筛选条件（可选）
const filter = ref({
    classReal: null,
    title: null,
    siteName: null,
    opPerson: null,
    createTime: null,
    pubTime: null,
    isPublished: null,
})


const getClassLabel = (classModelValue) => {
    const model = classModelList.find(item => item.value === classModelValue)
    return model ? model.label : '未知类型'
}

const getClassColor = (classModelValue) => {
    const model = classModelList.find(item => item.value === classModelValue)
    return model ? model.color : '#a8a8a8'
}

// 封装获取数据的函数，支持传入页码
const loadData = (page = 1) => {
    fetch(`/api/news/newsList?current=${page}&currentSize=${pageSize.value}`)
        .then(res => {
            if (!res.ok) {
                throw new Error('网络响应错误')
            }
            return res.json()
        })
        .then(res => {
            if (res.data && res.data.records) {
                newsList.value = res.data.records
                if (res.data.total) {
                    total.value = res.data.total
                }
                emits('updateNews', res.data.records)
            }
        })
        .catch(error => {
            console.error('获取新闻列表失败:', error)
        })
}

const handleItemClick=(item, index)=>{
    emits('click', item)
}

// 处理编辑按钮点击
const handleEdit = (item) => {
    emits('edit', item)
}


// 处理页码变化
const handlePageChange = (page) => {
    currentPage.value = page
    loadData(page)
}

// 初始化加载第一页数据
loadData()
</script>
<style scoped>
.news-container {
    padding: 20px;
    /* height: 100%; */
    overflow: auto;
}

.news-content {
    height: 130px;
    margin-top: 20px;
    border-bottom: 1px solid #eee;
}


.title-color {
    background-color: #e8954f;
    color: #fff;
    border-radius: 5px;
    padding: 3px 8px;
    margin-right: 10px;
}

.content-middle {
    padding: 15px 0;
    color: #a8a8a8;
}

.content-footer img {
    width: 30px;
    height: 30px;
    margin-right: 10px;
    border-radius: 50%;
}
</style>